import { controller, get, post } from '../middleware/route'
import request from '../utils/request'
import config from '../config'
let proxiy = 'http://cp-tools.cn'
@controller('/search')
export class Search {
  @get('/hotPlace')
  async hotPlace(ctx, next) {
    let city = ctx.store?ctx.store.api.position.city: ctx.query.city
    let options = {
      url: `${proxiy}/search/hotPlace`, 
      json: true, 
      qs: {sign: config.sign, city}
    }
    let res = await request(options)
    if (res.result.length != 0) {
      return (ctx.body = {
        code: 0,
        hotPlace: res.result
      })
    } else {
      return (ctx.body = {
        code: -1,
        hotPlace: []
      })
    }
  }
  
  @get('/top')
  async top(ctx, next) {
    let {city, input} = ctx.query
    let options = {
      url: `${proxiy}/search/top`, 
      json: true, 
      qs: {sign: config.sign, input, city}
    }
    let res = await request(options)
    if (res.top) {
      return (ctx.body = {
        code: 0,
        top: res.top
      })
    } else {
      return (ctx.body = {
        code: -1,
        top: []
      })
    }
  }

  @get('/searchByKeywords')
  async searchByKeywords(ctx, next) {
    let {city, keyword} = ctx.query
    let options = {
      url: `${proxiy}/search/resultsByKeywords`, 
      json: true, 
      qs: {sign: config.sign, keyword, city}
    }
    let res = await request(options)
    ctx.body = {
      code: res.count ? 0 : -1,
      count: res.count ? res.count : 0,
      pois: res.count ? res.pois : [], 
    }
    
  }

  @get('/product')
  async product(ctx, next) {
    let keyword = ctx.query.keyword || '旅游'
    let city = ctx.query.city || '广州'

    let options = {
      url: `${proxiy}/search/products`, 
      json: true, 
      qs: {sign: config.sign, keyword, city}
    }
    let res = await request(options)
    if (res.product) {
      ctx.body = {
        product: res.product,
        keyword,
        more: res.more
        // login: ctx.isAuthenticated()
        // more: ctx.isAuthenticated() ? res.more : [],
      }
    } else {
      ctx.body = {
        product: {},
        keyword,
        more: [],
        // login: ctx.isAuthenticated()
        // more: ctx.isAuthenticated() ? res.more: [],
        // login: ctx.isAuthenticated()
      }
    }
  }
}